#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MyString.h"

void InitString(MyStr *s)
{
    s->string = NULL;
    s->size = 0;
    s->len = 0;
}

bool InsertString(MyStr *s, const char *str)
{
    return InsertByIndex(s,str,s->len);
    // if(s->string == NULL)
    // {
    //     s->size = strlen(str)*2;
    //     s->string = (char *)malloc(s->size);
    //     if(s->string == NULL)
    //     {
    //         printf("InsertString malloc error!\n");
    //         s->size = 0;
    //         return false;
    //     }
    //     s->len = strlen(str);
    //     strcpy(s->string,str);
    //     return true;
    // }
    // else
    // {
    //     int newLen = strlen(str) + s->len;
    //     if(newLen+1 <= s->size)
    //     {
    //         s->len = newLen;
    //         strcat(s->string,str);
    //         return true;
    //     }
    //     else
    //     {
    //         int newSize = newLen * 2;
    //         char *newStr = realloc(s->string,newSize);
    //         if(newStr == NULL)
    //         {
    //             printf("InsertSting realloc error!\n");
    //             return false;
    //         }
    //         strcat(newStr,str);
    //         s->string = newStr;
    //         s->size = newSize;
    //         s->len = newLen;
    //         return true;
    //     }
    // }
    // return false;
}

bool InsertByIndex(MyStr *s, const char *str, int index)
{
    if(index < 0 || index > s->len)
    {
        printf("Index %d 是非法位置\n",index);
        return false;
    }
    //字符串为空
    if(s->string == NULL)
    {
        if(index != 0)
        {
            printf("Index %d 是非法位置\n",index);
            return false;
        }
        s->size = (strlen(str)+1)*2;
        s->string = (char *)malloc(s->size);
        if(s->string == NULL)
        {
            printf("IndexString malloc error!\n");
            s->size = 0;
            return false;
        }
        s->len = strlen(str);
        strcpy(s->string,str);
        return true;
    }
    else
    {
        int newLen = strlen(str) + s->len;
        if(newLen + 1 <= s->size)
        {
            // MyStr temp;
            // InitString(&temp);
            // InsertString(&temp,s->string+index);
            INSERTSTR(temp,s->string+index)
            s->string[index] = '\0';
            strcat(s->string,str);
            strcat(s->string,temp.string);
            free(temp.string);
            s->len = newLen;
            return true;
        }
        else
        {
            int newSize = newLen * 2;
            char *newStr = realloc(s->string,newSize);
            if(newStr == NULL)
            {
                printf("InsertString realloc error!\n");
                return false;
            }
            INSERTSTR(temp,newStr+index);
            newStr[index] = '\0';
            strcat(newStr,str);
            strcat(newStr,temp.string);
            free(temp.string);
            s->string = newStr;
            s->size = newSize;
            s->len = newLen;
            return true;
        }
    }
    return false;
}

bool InsertChar(MyStr *s, char c, int index)
{
    char temp[2] = {0};
    temp[0] = c;
    return InsertByIndex(s,temp,index);
}

bool RemoveString(MyStr *s, const char *str)
{
    char *target = strstr(s->string,str);
    if(target == NULL)
    {
        printf("没找到\n");
        return false;
    }
    char *back = target+strlen(str);
    while(*back != '\0')
    {
        *target++ = *back++;
    }
    *target = '\0';
    s->len = strlen(str);
    return false;
}

bool IsContains(MyStr *s, const char *str)
{
    if(strstr(s->string,str) == NULL)
    {
        return false;
    }
    return true;
}

bool IsEqual(MyStr *s, const char *str)
{
    if(strcmp(s->string,str) == 0)
    {
        return true;
    }
    return false;
}

bool IsEqualCaseInsensitive(MyStr *s, const char *str)
{
    MyStr s1 = ToLower(s->string);
    MyStr s2 = ToLower(str);
    if(strcmp(s1.string,s2.string) == 0)
    {
        ClearString(&s1);
        ClearString(&s2);
        return true;
    }
    ClearString(&s1);
    ClearString(&s2);
    return false;
}

MyStr ToUpper(const char *str)
{
    INSERTSTR(s,str);
    char *temp = s.string;
    while(*temp != '\0')
    {
        if(*temp <= 'z' && *temp >= 'a')
        {
            *temp -= 32;
        }
        temp++;
    }
    return s;
}

MyStr ToLower(const char *str)
{
    INSERTSTR(s,str);
    char *temp = s.string;
    while(*temp != '\0')
    {
        if(*temp <='Z' && *temp >= 'A')
        {
            *temp += 32;
        }
        temp++;
    }
    return s;
}

int StringToInt(MyStr *s)
{
    return atoi(s->string);
}

double StringToDouble(MyStr *s)
{
    return atof(s->string);
}

MyStr CopyString(const MyStr *s)
{
    INSERTSTR(newStr,s->string)
    return newStr;
}

MyStr GetString(const char *s)
{
    INSERTSTR(temp,s);
    return temp;
}

MyStrList Split(const char *src, const char *demil)
{
    MyStrList list;
    InitMyStringList(&list);

    MyStr temp = GetString(src);
    char *str = strtok(temp.string,demil);
    while(str != NULL)
    {
        InsertTail(&list,str);
        str = strtok(NULL,demil);
    }
    ClearString(&temp);
    return list;
}

void ClearString(MyStr *s)
{
    free(s->string);
    InitString(s);
}

void InitMyStringList(MyStrList *list)
{
    list->StringList = NULL;
    list->len = 0;
    list->size = 0;
}

bool InsertTail(MyStrList *list, const char *str)
{
    if(list->len == list->size)
    {
        int newSize = (list->len + 1) * 2;
        MyStr *newStrList = realloc(list->StringList,newSize * sizeof(MyStr));
        if(newStrList == NULL)
        {
            printf("InsertStrList realloc error!\n");
            return false;
        }
        list->StringList = newStrList;
        list->size = newSize;
    }
    list->StringList[list->len++] = GetString(str);
    return true;
}

bool RemoveMyStrByIndex(MyStrList *list, int index)
{
    if(index < 0 || index >= list->len)
    {
        printf("删除非法位置\n");
        return false;
    }
    for(int i = index; i < list->len-1; i++)
    {
        list->StringList[i] = list->StringList[i+1];
    }
    list->len--;
    return true;
}

bool RemoveMySting(MyStrList *list, const char *str)
{
    for(int i = 0;i < list->len;i++)
    {
        if(IsEqual(&list->StringList[i],str) == true)
        {
            if(RemoveMyStrByIndex(list,i) ==  false)
            {
                return false;
            }
            i--;
        } 
    }
    return true;
}

void DisplayStringList(MyStrList *list)
{
    for(int i = 0; i < list->len; i++)
    {
        printf("%s\n",list->StringList[i].string);
    }
}

void ClearMyStringList(MyStrList *list)
{
    for(int i = 0; i < list->len; i++)
    {
        ClearString(&list->StringList[i]);
    }
    free(list->StringList);
    InitMyStringList(list);
}
                                                                                                        